'''视频里面提取图片'''
'''等会要用'''
import cv2
import time
import os

input_video = r'/home/xys/CloundShiProjects/xitieshan/videos/hous_video/新建文件夹/'
output_dir = r'/home/xys/CloundShiProjects/xitieshan/images/dataset_image/image_new/'

def video_duration(filename):
    cap = cv2.VideoCapture(filename)
    if cap.isOpened():
        rate = cap.get(5)
        frame_num = cap.get(7)
        duration = frame_num / rate
        return duration
    return -1


x=0
count = 0
for video in os.listdir(input_video):
    vidcap = cv2.VideoCapture(input_video+video)
    START_TIME = 1  # 设置开始时间(单位秒)
    END_TIME = video_duration(input_video+video)  # 设置结束时间(单位秒)
    fps = int(vidcap.get(cv2.CAP_PROP_FPS))  # 获取视频每秒的帧数
    frameToStart = START_TIME * fps  # 开始帧 = 开始时间*帧率
    frametoStop = END_TIME * fps  # 结束帧 = 结束时间*帧率
    # vidcap.set(cv2.CAP_PROP_POS_FRAMES, frameToStart)  # 设置读取的位置,从第几帧开始读取视频
    success, image = vidcap.read()  # 获取第一帧
    x+=1
    print(x)
    seconds = 1
    while success:
        if count % int(fps * seconds) == 0:  # 每second秒保存一次
            save_path = output_dir + str(count) + ".jpg"
            cv2.imwrite(save_path, image)  # 保存图片
            print('Process %dth seconds: ' % int(count / (fps * seconds)), success)
        success, image = vidcap.read()  # 每次读取一帧
        count += 1
print("end!")
